package cmd

import (
	"fmt"
	"os"
	"strings"

	"gitee.com/workits/pkgs/toolx"
	"gitee.com/workits/pkgs/utilx"
	"github.com/fzdwx/infinite/components"
	"github.com/spf13/cobra"
)

var initCmd = &cobra.Command{
	Use:   "init",
	Short: "初始化项目环境",
	Long:  "初始化项目环境",
	Run: func(cmd *cobra.Command, args []string) {
		// 判断是否已存在安装
		reinit := false
		if utilx.FsIsExist("." + toolx.PS + ".workits") {
			existInput := components.NewInput()
			existInput.Prompt = "项目已经被初始化过, 是否要将模版更至新最新版本(y/n)? "
			_ = components.NewStartUp(existInput).Start()
			if strings.ToLower(existInput.Value()) != "y" {
				os.Exit(0)
			}
			reinit = true
		}

		toolx.CopyTemplates(".", reinit)
		if reinit {
			fmt.Println("🎉项目环境模版已更新至最新")
		} else {
			toolx.CreateConfig(".")
			fmt.Println("🎉项目环境初始化完成")
		}
	},
}

func init() {
	rootCmd.AddCommand(initCmd)
}
